package feizhou.untils.tools.io;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class XmlUtil {
	
	private static Logger logger = Logger.getLogger(XmlUtil.class);
	private static Map<String, Object> map = new HashMap<>();

	public static void initXml(String fileName){
		try {
			Document document = new SAXReader().read(XmlUtil.class.getClassLoader().getResourceAsStream(fileName));
			@SuppressWarnings("unchecked")
			Iterator<Element> it = document.getRootElement().elementIterator();
			while (it.hasNext()) {
				Element e = it.next();
				if(e.getName().equals("errCode")){
					map.put(e.attributeValue("key"), e.getText());
				}else {
					//自定义标签，不取属性，直接取值或者子元素值
					Iterator<Element> iterator = e.elementIterator();
					Map<String,String>subItem=new HashMap<>();
					while (iterator.hasNext()){
						Element next = iterator.next();
						subItem.put(next.getName(),next.getStringValue());
					}
					map.put(e.getName(),subItem);
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);;
		}
	}
	public static String getErrMsg(Long key) {
		return (String) map.get(key.toString());
	}
	public static String getErrMsg(String key) {
		return (String) map.get(key);
	}

	public static Map<String,String>getSubItems(String key){
		return (Map<String, String>) map.get(key);
	}
    /**
    *
    * @param str  需要处理的字符串
    * @param tag 标签名称
    * @param tagAttrib 要替换的标签属性值
    * @param startTag 新标签开始标记
    * @param endTag  新标签结束标记
    * @return
    */
   public static String replaceHtmlTag(String str, String tag, String tagAttrib, String startTag, String endTag) {
       String regxpForTag = "<\\s*" + tag + "\\s+([^>]*)\\s*" ;
       String regxpForTagAttrib = tagAttrib + "=\\s*\"([^\"]+)\"" ;
       Pattern patternForTag = Pattern.compile (regxpForTag,Pattern. CASE_INSENSITIVE );
       Pattern patternForAttrib = Pattern.compile (regxpForTagAttrib,Pattern. CASE_INSENSITIVE );
       Matcher matcherForTag = patternForTag.matcher(str);
       StringBuffer sb = new StringBuffer();
       boolean result = matcherForTag.find();
       while (result) {
           StringBuffer sbreplace = new StringBuffer( "<"+tag+" ");
           Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group(1));
           if (matcherForAttrib.find()) {
               String attributeStr = "";//这个可以实现src路径拼接 方法： String attributeStr = matcherForAttrib.group(1);
               matcherForAttrib.appendReplacement(sbreplace, startTag + attributeStr + endTag);
           }
           matcherForAttrib.appendTail(sbreplace);
           matcherForTag.appendReplacement(sb, sbreplace.toString());
           result = matcherForTag.find();
       }
       matcherForTag.appendTail(sb);
       return sb.toString();
   }
   public static void main(String[] args) {
	   StringBuffer content = new StringBuffer();
	   content.append("<ul class=\"imgBox\"><li><img id=\"160424\" src=\"uploads/allimg/160424/1-160424120T1-50.jpg\" class=\"src_class\"></li>");
	   content.append("<li><img id=\"150628\" src=\"uploads/allimg/150628/1-15062Q12247.jpg\" class=\"src_class\"></li></ul>");
	   System.out.println("原始字符串为:"+content.toString());
	   //String newStr = replaceHtmlTag(content.toString(), "img", "src", "src=\"http://junlenet.com/", "\"");
	   String newStr = replaceHtmlTag(content.toString(), "img", "src", "src=\"********", "\"");
	   System.out.println("替换后为:"+newStr);
   }
}
